

let activity_tag = 'chinaso'
let request_log = ""
let request_key = activity_tag+'_award'
let skill_count_key = activity_tag+'_award_count'
let request_log_key = activity_tag+'_award_log'
let skill_name = 'skill_name'
let skill_result = false
let default_count = 3
let availableTimes = 20
function rewrite_body(body) {
    //body =  JSON.parse(body);
    body = body.replaceAll('10:00:00', '01:00:00')
    body = body.replaceAll('时藏专属区块链身份勋章', '芭比Q')
    // body =  JSON.stringify(body);
    return body
}  

function resp_case(request_value,count,res) {
    console.log('请求结果：');
    console.log(res.headers.date);
    console.log(res.data);
    if(res.data.msg.includes('成功')){
        skill_result = true
       
    }else{
        axios_skill(request_value,count)
    }

    if(!request_log.includes(res.data.msg)){
        request_log= request_log + res.headers.date+":"+res.data.msg +"\n"
        $store.set(request_log_key, request_log) 
    }
  
}
function error_case(request_value,count,e) {
    console.log(e.message)
    axios_skill(request_value,count)
}


function get_request_value() {
    let request_value ={
        "hostname": "collection.chinaso.com",
        "port": 443,
        "path": "/collection-app/order/create",
        "method": "POST",
        "headers": {
          "Host": "collection.chinaso.com",
          "Content-Type": "application/json",
          "Origin": "https://collection.chinaso.com",
          "Accept-Encoding": "gzip, deflate, br",
          "Cookie": "",
          "Connection": "keep-alive",
          "Accept": "application/json, text/plain, */*",
          "User-Agent": "",
          "Referer": "https://collection.chinaso.com/front/indexDetail?id=36",
          "Accept-Language": "zh-cn"
        },
        "protocol": "https",
        "url": "https://collection.chinaso.com/collection-app/order/create",
        "body": "{\"collectionSetId\":58,\"productId\":36,\"payMethod\":\"WXPAY\"}"
      }
    let request_param = $store.get(request_key);
    if(request_param&&request_param!=""){
        request_value.headers.Cookie=request_param.headers.Cookie
        request_value.headers['User-Agent']=request_param.headers['User-Agent']
    }else{
        request_value = ""
    }
   
    return request_value
}




//上面是暂时需要适配的部分
!(async () => {
    if (typeof $request !== "undefined") {
  console.log(`开始获取请求参数`);
   get_request_key();
   let body = $response.body;
   body= rewrite_body(body);
    $done({ body })
}else {
    console.log(`开始执行请求`);
    request_log  = $store.get(skill_name)+'\n';
    $store.set(request_log_key, request_log) 
    let request_value = get_request_value();
    if(request_value&&request_value!=""){
        //  $store.delete没有暴露出来，无法使用，兼容下
        // $store.set(request_key, "")  
        let count = $store.get(skill_count_key);
        if (!count){
            $store.set(skill_count_key, default_count)  
            count = default_count
        }
        // for (let j = 0; j < availableTimes; j++) {
        
        //     setTimeout(axios_skill,j*300+500,request_value,count);
        // }
        axios_skill(request_value,count);
        
    }else{
        console.log(request_key+`不存在`);
    }
    }
    })()
   .catch((e) =>console.log(e))
   .finally(() =>$done())

function axios_skill(request_value,count){
    if(!skill_result&&count>0){
        console.log(`执行请求：`);
        count--;
        $axios(request_value)
        .then(res=>resp_case(request_value,count,res))
        .catch(e=>error_case(request_value,count,e));  
    }
  
}


function get_request_key() {
    let req = $request;
    delete req.bodyBytes
    $store.set(request_key,req) 
}
